<?php

namespace App\Console\Commands;

use App\Models\Order\Saleorder;
use App\Models\Order\Salesordersub;
use App\Models\Purchase\PurchaseWarehousingOrderSub;
use App\Models\ReportForms\PriceForms as ModelPriceForms;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\DB;

class PriceForms extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:PriceForms';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '价格表报';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        Log::info('价格表报' . time());

        $startTime = strtotime(date('Y-m') . '-1 00:00:00');
        $endTime = strtotime(date('Y-m-d') . ' 23:59:59');

        $res = Salesordersub::select(DB::raw('SUM(weight) as total_weight,SUM(performance) as total_performance'))
            ->where([
                'create_at' => [
                    ['>=', $startTime],
                    ['<=', $endTime],
                ],
                'produce_status' => 2,
                'is_del' => 1,
            ])->get();

        $saleCount = Saleorder::where([
            'create_at' => [
                ['>=', $startTime],
                ['<=', $endTime],
            ],
            'produce_status' => 2,
            'is_del' => 1,
        ])->count();



        $scheduled_receipt = PurchaseWarehousingOrderSub::with(['mainorder'])->whereHas('mainorder', function ($query) use ($startTime, $endTime) {
            $query->where('audit_status', 3)->where('is_del', 1)->where([
                ['create_at', '>=', $startTime],
                ['create_at', '<=', $endTime],
            ]);
        })->sum('total_weight');


        ModelPriceForms::create([
            'date' => date('Y-m'),
            'scheduled_receipt' => intval($scheduled_receipt),
            'outbound_quantity' => intval($res[0]['total_weight']),
            'sale_performance' => intval($res[0]['total_performance']),
            'sale_order_number' => intval($saleCount),
            'create_at' => time(),
        ]);


        /**
         * CREATE TABLE `huakun_price_forms` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `cpid` int(10) NOT NULL DEFAULT '0',
  `date` varchar(50) NOT NULL DEFAULT '' COMMENT '日期，Y-m-d',
  `purchase_price` int(11) NOT NULL DEFAULT '0' COMMENT '采购均价',
  `sale_price` int(11) NOT NULL DEFAULT '0' COMMENT '销售均价',
  `website_price` int(11) NOT NULL DEFAULT '0' COMMENT '网站价格',
  `operate_price` int(11) NOT NULL DEFAULT '0' COMMENT '操盘定价',
  `future_price` int(11) NOT NULL DEFAULT '0' COMMENT '期货价格',
  `diff_price` int(11) NOT NULL DEFAULT '0' COMMENT '期现基差',
  `create_at` int(10) unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`,`cpid`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=31 DEFAULT CHARSET=utf8mb4 ROW_FORMAT=COMPACT COMMENT='价格';
         */

        return 0;
    }
}
